package org.itstack.demo.design;

import org.itstack.demo.design.store.CommodityTypeEnum;
import org.itstack.demo.design.store.ICommodity;
import org.itstack.demo.design.store.impl.CardCommodityService;
import org.itstack.demo.design.store.impl.CouponCommodityService;
import org.itstack.demo.design.store.impl.GoodsCommodityService;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class StoreFactory {

    private static Map<CommodityTypeEnum, ICommodity> commodityServiceMap = new HashMap<>();

    public StoreFactory() {
        commodityServiceMap.put(CommodityTypeEnum.COUPON, new CouponCommodityService());
        commodityServiceMap.put(CommodityTypeEnum.CARD, new CardCommodityService());
        commodityServiceMap.put(CommodityTypeEnum.GOODS, new GoodsCommodityService());
    }

    public ICommodity getCommodityService(CommodityTypeEnum commodityType) {
        ICommodity iCommodity = commodityServiceMap.get(commodityType);
        if (Objects.isNull(iCommodity)) {
            throw new RuntimeException("不存在的商品服务类型");
        }
        return iCommodity;
    }

}
